package edu.ncsu.csc.itrust.dao.officevisit;

import java.util.List;
import edu.ncsu.csc.itrust.beans.InstructionBean;
import edu.ncsu.csc.itrust.dao.mysql.OfficeVisitInstructionDAO;
import edu.ncsu.csc.itrust.datagenerators.TestDataGenerator;
import edu.ncsu.csc.itrust.exception.DBException;
import edu.ncsu.csc.itrust.testutils.EvilDAOFactory;
import edu.ncsu.csc.itrust.testutils.TestDAOFactory;
import junit.framework.TestCase;

/**
 * Test the InstructionsDAO
 * @author Patrick Finegan
 *
 */
public class OVInstructionsTest extends TestCase {

	private OfficeVisitInstructionDAO oviDAO = TestDAOFactory.getTestInstance().getOVInstructionDAO();
	private OfficeVisitInstructionDAO eviloviDAO = EvilDAOFactory.getEvilInstance().getOVInstructionDAO();	
	private InstructionBean ibean;
	private long id = -1;
	
	public void setUp() throws Exception{
		TestDataGenerator gen = new TestDataGenerator();
		gen.clearAllTables();
		gen.standardData();
		
		id = -1;
		
		ibean = new InstructionBean();
		ibean.setInstructID(4);
		ibean.setHCPID(9000000000L);
		ibean.setPID(2);
		ibean.setDescriptName("Test1");
		ibean.setURL("http://moodle.wolfware.ncsu.edu");
		ibean.setComments("Test1 Comments");
		ibean.setOVID(955);
		ibean.setModDate("2007-6-12 10:35:00");
	}
	
	/**
	 * Tests all of the SQL Exceptions
	 * @throws Exception
	 */
	public void testSQLFails() throws Exception{
		long instID = 0;
		try{
			instID= eviloviDAO.addInstruction(ibean);
			fail();
		}catch(DBException e){
			//should get to here
		}
		ibean.setDescriptName("Test11");
		try{
			eviloviDAO.editInstruction(ibean);
			fail();
		}catch(DBException e){
			//should get here
		}
		try{
			eviloviDAO.removeInstruction(ibean.getInstructID());
			fail();
		}catch(DBException e){
			//should get here
		}
	}
	
	public void testAddInstruction() throws Exception{
		id = oviDAO.addInstruction(ibean);
		assertFalse(id == -1);
	}
	
	public void testEditInstruction() throws Exception{
		ibean.setDescriptName("Test11");
		id = oviDAO.editInstruction(ibean);
		assertFalse( id == -1);
	}
	
	public void testremoveInstruction() throws Exception{
		oviDAO.removeInstruction(ibean.getInstructID() );
		List<InstructionBean> bean = oviDAO.getInstructionForPatient(2);
		assertEquals( 1, bean.size() );
	}
	
	public void testGetByX() throws Exception{
		List<InstructionBean> bean = oviDAO.getInstructionForPatient(2);
		assertEquals( 1, bean.size() );
		
		List<InstructionBean> bean2 = oviDAO.getInstructionForOfficeVisit(955);
		assertEquals( 1, bean2.size() );
	}
}
